from typing import Dict
import re


class Expositor:
    def __init__(self) -> None:
        self.dictionary = {
            "开盘价":"open",
            "收盘价":"close",
            "最高价":"high",
            "最低价":"low",
            "上涨":"rise",
            "下跌":"fall",
            "购买":"BUY",
            "出售":"SELL",
            "浪潮信息":"000977.SZ",
            "协鑫能科":"002015.SZ",
            "北京君正":"300223.SZ",
            "比音勒芬":"002832.SZ"
        }
        return

    def itemDescriptionParse(self,description:str)->Dict:
        pattern = r'事件发生时(.*)(上涨|下跌|震荡|稳定)'
        match = re.search(pattern, description)
           # 如果匹配成功，提取匹配数据
        temp_condition = {
            "label":None,
            "value":None,
        }
        if match:
            temp_condition['label'] = match.group(1)
            temp_condition['value'] = match.group(2)
            return temp_condition
        else:
            return "Unable to parse sentence."

    def stockStrategyDscriptionParse(self,description:str,stock_code:str)->Dict:
        
        parts = description.split('，')
        condition_description_list = parts[0].split('如果：')[1].split('/')
        operation_description = parts[1].split('那么：')[1]
        temp_strategy = {
            'condition_list':[],
            'operation':{}
        }
        for i in condition_description_list:
            temp_condition = {}
            if i[:3] == '该股票':
                pattern = r"该股票((?:开盘价|收盘价|最高价|最低价))前(\d+)日((?:上涨|下跌))超过(\d+\.\d+)%"
                match = re.search(pattern, i)
                temp_condition['attribute_source'] = stock_code
                temp_condition['attribute_name'] = self.dictionary[match.group(1)]
                temp_condition['calculate_type'] = self.dictionary[match.group(3)]
                temp_condition['offdays'] = int(match.group(2))
                temp_condition['variation'] = float(match.group(4))
            else:
                pattern = r'(.*)前(\d)日(?:(上涨|下跌))超过(\d+\.\d+)%'
                match = re.search(pattern,i)
                temp_condition['attribute_source'] = 'scenario'
                temp_condition['attribute_name'] = match.group(1)
                temp_condition['calculate_type'] = self.dictionary[match.group(3)]
                temp_condition['offdays'] = int(match.group(2))
                temp_condition['variation'] = float(match.group(4))
            temp_strategy['condition_list'].append(temp_condition)
        
            operation_description = operation_description.replace("该股票", '{'+stock_code+'}')
            pattern = r"(购买|出售)\{([^{}]*)\}(\d+)(只|股)"
            match = re.match(pattern, operation_description)
            temp_strategy['operation']['operation_type'] = self.dictionary[match.group(1)]
            temp_strategy['operation']['stock_code'] = match.group(2)
            temp_strategy['operation']['amount'] = int(match.group(3))
        return temp_strategy
    
    def portfolioStrategyDescriptionParse(self,description:str)->Dict:
        parts = description.split('，')
        condition_description_list = parts[0].split('如果：')[1].split('/')
        operation_description = parts[1].split('那么：')[1]
        temp_strategy = {
            'condition_list':[],
            'operation':{}
        }
        for i in condition_description_list:
            temp_condition = {}
            pattern = r'(\w+)((?:开盘价|收盘价|最高价|最低价))前(\d)日(?:(上涨|下跌))超过(\d+\.\d+)%'

            match = re.search(pattern,i)
            temp_condition['attribute_source'] = self.dictionary[match.group(1)]
            temp_condition['attribute_name'] = self.dictionary[match.group(2)]
            temp_condition['calculate_type'] = self.dictionary[match.group(4)]
            temp_condition['offdays'] = int(match.group(3))
            temp_condition['variation'] = float(match.group(5))
            temp_strategy['condition_list'].append(temp_condition)
        
            pattern = r"(购买|出售)(\D+?)(\d+)(只|股)"
            match = re.match(pattern, operation_description)
            temp_strategy['operation']['operation_type'] = self.dictionary[match.group(1)]
            temp_strategy['operation']['stock_code'] = self.dictionary[match.group(2)]
            temp_strategy['operation']['amount'] = int(match.group(3))
            
        return temp_strategy

    